<template>
  <view style="padding-bottom: 160rpx;">
    <u-navbar
      :customBack="backToList"
      v-if="!$isWeixin()"
      :title="title"
      :background="{ backgroundColor: '#ffffff' }"
      :titleBold="true"
      :is-fixed="true"
    ></u-navbar>
    <u-tabs :list="tabList" active-color="#FF7A23" :current="tabIndex" @change="onTabChange"></u-tabs>

    <view v-show="tabIndex === 0">
      <u-cell-group>
        <u-cell-item :arrow="false" title="申请类型" value="新增"></u-cell-item>
        <u-cell-item :arrow="false" title="申请原因">
          <u-radio-group v-model="form.ApplyForReasonId">
            <view class="grid-2">
              <u-radio v-for="(item, index) in DataBaseInfo.DealerApplyFor_BaseList" :name="item.Key" :key="index">
                {{ item.Value }}
              </u-radio>
            </view>
          </u-radio-group>
        </u-cell-item>
        <u-cell-item v-if="form.ApplyForReasonId == 3" :arrow="false" title="物料需求">
          <u-checkbox-group @change="onRequirementsChange">
            <view class="grid-2">
              <u-checkbox
                v-model="item.checked"
                v-for="(item, index) in DataBaseInfo.DealerMaterialRequirements_BaseList"
                :name="item.Key"
                :key="index"
              >
                {{ item.Value }}
              </u-checkbox>
            </view>
          </u-checkbox-group>
        </u-cell-item>
        <u-cell-item v-if="form.ApplyForReasonId == 4" :arrow="false" title="费用需求">
          <u-checkbox-group @change="onRequirementsChange">
            <view class="grid-2">
              <u-checkbox
                v-model="item.checked"
                v-for="(item, index) in DataBaseInfo.DealerMoneyRequirements_BaseList"
                :name="item.Key"
                :key="index"
              >
                {{ item.Value }}
              </u-checkbox>
            </view>
          </u-checkbox-group>
        </u-cell-item>
        <u-cell-item v-if="form.ApplyForReasonId == 5" :arrow="false" title="其他备注">
          <u-input :autoHeight="true" type="textarea" inputAlign="right" placeholder="其他备注"></u-input>
        </u-cell-item>
        <u-cell-item :arrow="false" title="申请时间">
          <u-input
            :value="$u.date(form.Date * 1000, 'yyyy-mm-dd')"
            inputAlign="right"
            placeholder="请选择"
            disabled
            @click="showTimePick = true"
            type="select"
            :selectOpen="showTimePick"
          ></u-input>
          <u-picker
            :default-time="$u.date(form.Date * 1000, 'yyyy-mm-dd')"
            @confirm="onTimePickerChange"
            v-model="showTimePick"
            mode="time"
            :params="{ year: true, month: true, day: true, hour: false, minute: false, second: false }"
          ></u-picker>
        </u-cell-item>
      </u-cell-group>

      <u-gap height="40" bg-color="#f5f5f5"></u-gap>
      <review-form ref="reviewForm"></review-form>
    </view>

    <relations v-if="tabIndex === 1" :list="This_Dealer_RelationList"></relations>

    <view v-show="tabIndex === 2">
      <u-cell-group :border="false">
        <u-cell-item
          v-if="ShowLevel != 1"
          :border-bottom="false"
          :arrow="false"
          title="一级经销商名称"
          :value="FirstDealerInfo.Dealer_BaseInfo.Name"
        ></u-cell-item>
        <u-cell-item
          v-if="ShowLevel == 3"
          :border-bottom="false"
          :arrow="false"
          title="二级经销商名称"
          :value="DoubleDealerInfo.Dealer_BaseInfo.Name"
        ></u-cell-item>
      </u-cell-group>

      <u-cell-item :arrow="false" title="门店属性" :border-bottom="false">
        <u-radio-group v-model="form.OldDoor">
          <u-radio name="1">门店新增</u-radio>
          <u-radio name="2">老店新开</u-radio>
        </u-radio-group>
      </u-cell-item>

      <basic-information-form
        v-show="ShowLevel != 3"
        :loadCity="loadCity"
        :level="ShowLevel"
        :showLevel="DealerGrade_Fill_In_Permissions > 0"
        ref="basicForm"
        :metadata="DataBaseInfo"
      ></basic-information-form>

      <store-information-form
        v-show="ShowLevel == 3"
        :showGrade="DealerGrade_Fill_In_Permissions > 0"
        :metadata="DataBaseInfo"
        :loadCity="loadCity"
        ref="storeForm"
      ></store-information-form>
    </view>

    <view class="" v-show="tabIndex == 3 && ShowLevel == 3">
      <new-store-photo v-model="form.InitialStorePhoto"></new-store-photo>
    </view>

    <view class="u-p-30 u-flex">
      <u-button @tap="backToList">返回列表</u-button>
      <u-button type="warning" @tap="onSubmit">提交审核</u-button>
    </view>

    <u-toast ref="uToast" />
  </view>
</template>

<script>
export default {
  data() {
    return {
      tabIndex: 0,
      tabList: [{ name: '申请信息' }, { name: '销售业务关系' }, { name: '基本信息' }],
      showTimePick: false,
      loadCity: false,
      title: '',
      Id: '',
      DealerId: '',
      ApplyForType: '',

      ShowLevel: 0,

      This_Dealer_RelationList: [],

      DataBaseInfo: {
        BusinessCategory_BaseList: [],
        CompetingGoodsBrandA_BaseList: [],
        CompetingGoodsBrandB_BaseList: [],
        CompetingGoodsBrandC_BaseList: [],
        DealerApplyFor_BaseList: [],
        DealerGrade_BaseList: [],
        DealerMaterialRequirements_BaseList: [],
        DealerMoneyRequirements_BaseList: [],
        DealerShutDown_BaseList: [],
        DealerType_BaseList: [],
        DealerAndGoReason_BaseList: []
      },

      DealerGrade_Fill_In_Permissions: 0,

      FirstDealerInfo: {
        Dealer_BaseInfo: {
          Account: 'D02901',
          Address: '唐延路35号旺座现代城B座2701室',
          CityId: 610100,
          Created: 1460613600,
          DealerId: 114,
          Delivery_Address: '唐延路35号旺座现代城B座2701室',
          Delivery_CityId: 610100,
          Delivery_DistrictId: 659020,
          Delivery_ProvinceId: 610000,
          DistrictId: 659020,
          IsFictitious: 0,
          KeDealerNo: '01000090',
          Level: 1,
          LevelName: '一级',
          Mobile: '18629309637',
          Name: '',
          NickName: '党元斌测试',
          Parent_DealerId: 90,
          Parent_Name: '喜德瑞',
          ProvinceId: 610000,
          Referred: '西安元聚',
          State: 4,
          Telephone: ''
        },
        Dealer_OtherInfo: {
          BlessingNote: '测试1111112',
          BossBirthday: 1626192000,
          BossName: '高方明',
          BossPhone: '15007142474',
          BusinessCategory: '22,23,',
          BusinessLicenseA: 'o_1e6b7tmbk4of12fr1muqli1ofg10.jpg',
          CompetingGoodsBrandA: '5,6,8,',
          CompetingGoodsBrandB: '47,49,50,',
          CompetingGoodsBrandC: '64,65,67,70,71,',
          DealerGrade: 1,
          DealerType: 1,
          IsSign: 2,
          IsVIP: 1,
          LastYearsBDRSales: 14001,
          PeopleNum: 25,
          PercentageBDRSales: 35,
          TotalSales: 40001,
          TreatyPhoto: 'o_1ed682sou1iuf2lmc37rc31jjjd.png',
          TreatyPhotoType: 1
        },
        Dealer_RelationList: []
      },
      DoubleDealerInfo: {
        Dealer_BaseInfo: {
          Account: 'D02901',
          Address: '唐延路35号旺座现代城B座2701室',
          CityId: 610100,
          Created: 1460613600,
          DealerId: 114,
          Delivery_Address: '唐延路35号旺座现代城B座2701室',
          Delivery_CityId: 610100,
          Delivery_DistrictId: 659020,
          Delivery_ProvinceId: 610000,
          DistrictId: 659020,
          IsFictitious: 0,
          KeDealerNo: '01000090',
          Level: 1,
          LevelName: '一级',
          Mobile: '18629309637',
          Name: '',
          NickName: '党元斌测试',
          Parent_DealerId: 90,
          Parent_Name: '喜德瑞',
          ProvinceId: 610000,
          Referred: '西安元聚',
          State: 4,
          Telephone: ''
        },
        Dealer_OtherInfo: {
          BlessingNote: '测试1111112',
          BossBirthday: 1626192000,
          BossName: '高方明',
          BossPhone: '15007142474',
          BusinessCategory: '22,23,',
          BusinessLicenseA: 'o_1e6b7tmbk4of12fr1muqli1ofg10.jpg',
          CompetingGoodsBrandA: '5,6,8,',
          CompetingGoodsBrandB: '47,49,50,',
          CompetingGoodsBrandC: '64,65,67,70,71,',
          DealerGrade: 1,
          DealerType: 1,
          IsSign: 2,
          IsVIP: 1,
          LastYearsBDRSales: 14001,
          PeopleNum: 25,
          PercentageBDRSales: 35,
          TotalSales: 40001,
          TreatyPhoto: 'o_1ed682sou1iuf2lmc37rc31jjjd.png',
          TreatyPhotoType: 1
        }
      },

      form: {
        DealerId: null,
        Level: 1,
        ApplyForType: 1,
        ApplyForReasonId: null,
        Date: parseInt(new Date().getTime() / 1000),
        Requirements: '',
        DealerGrade: '',
        Remark: '',
        OtherNote: '',
        InitialStorePhoto: [],
        AuditRemark: '',
        State: '',
        OldDoor: ''
      }
    };
  },

  onLoad(options) {
    if (options.DealerId) {
      this.form.DealerId = options.DealerId;
    }
    if (options.ApplyType) {
      this.form.ApplyForType = options.ApplyForType;
    }
    if (options.Id) {
      this.form.Id = options.Id;
    }
    this.getData();

    this.title = '新增经销商审核';
    if (this.$isWeixin()) {
      this.$setTitle(this.title);
    }
  },

  methods: {
    backToList() {
      uni.navigateBack();
    },

    onTabChange(index) {
      this.tabIndex = index;
    },

    onRequirementsChange(e) {
      this.form.Requirements = e.toString();
    },

    onTimePickerChange(e) {
      this.form.Date = new Date(`${e.year}-${e.month}-${e.day}`).getTime() / 1000;
    },

    getData() {
      this.$u.api
        .getDealerChangeReviewDetail({
          AdjustmentId: parseInt(this.form.Id),
          ApplayTypeId: parseInt(this.form.ApplyForType),
          DealerId: parseInt(this.form.DealerId)
        })
        .then(res => {
          const {
            ResultCode,
            ResultMessage,
            ShowLevel,
            AuditDetail,
            First_Dealer_BaseInfo,
            Double_Dealer_BaseInfo,
            Door_Dealer_BaseInfo,
            This_Dealer_OtherInfo,
            This_Dealer_RelationList,
            DataBaseInfo,
            AndGoParentId_List,
            AndGoSecondId_List,
            AuditForList,
            InitialStorePhoto,
            DealerGrade_Fill_In_Permissions
          } = res;

          if (ResultCode === 1) {
            this.ShowLevel = ShowLevel;

            if (DealerGrade_Fill_In_Permissions) {
              this.DealerGrade_Fill_In_Permissions = DealerGrade_Fill_In_Permissions;
            }

            if (ShowLevel == 3) {
              this.tabList.push({ name: '门店开户照片' });
            }

            if (InitialStorePhoto) {
              this.form.InitialStorePhoto = InitialStorePhoto;
            }

            this.form.ApplyForReasonId = AuditDetail.ApplyForReasonId;

            this.First_Dealer_BaseInfo = First_Dealer_BaseInfo;
            this.Double_Dealer_BaseInfo = Double_Dealer_BaseInfo;
            this.AuditForList = AuditForList;

            if (Door_Dealer_BaseInfo) {
              this.Door_Dealer_BaseInfo = Door_Dealer_BaseInfo;
            }
            this.This_Dealer_OtherInfo = This_Dealer_OtherInfo;
            this.This_Dealer_RelationList = This_Dealer_RelationList;
            this.DataBaseInfo = DataBaseInfo;

            this.form.OldDoor = This_Dealer_OtherInfo.OldDoor;

            this.AndGoParentId_List = AndGoParentId_List;
            this.AndGoSecondId_List = AndGoSecondId_List;

            this.FirstDealerInfo.Dealer_BaseInfo = First_Dealer_BaseInfo;
            this.DoubleDealerInfo.Dealer_BaseInfo = Double_Dealer_BaseInfo;

            if (this.DataBaseInfo.DealerMoneyRequirements_BaseList) {
              this.DataBaseInfo.DealerMoneyRequirements_BaseList.forEach(item => {
                item.checked = AuditDetail.DemandId.split(',').indexOf(String(item.Key)) > -1;
              });
            }

            if (this.DataBaseInfo.DealerMaterialRequirements_BaseList) {
              this.DataBaseInfo.DealerMaterialRequirements_BaseList.forEach(item => {
                item.checked = AuditDetail.DemandId.split(',').indexOf(String(item.Key)) > -1;
              });
            }

            if (ShowLevel == 1) {
              this.FirstDealerInfo.Dealer_OtherInfo = This_Dealer_OtherInfo;

              this.$refs.basicForm.initForm({
                Dealer_BaseInfo: First_Dealer_BaseInfo,
                Dealer_OtherInfo: This_Dealer_OtherInfo
              });
            }

            if (ShowLevel == 2) {
              this.DoubleDealerInfo.Dealer_OtherInfo = This_Dealer_OtherInfo;
              this.DataBaseInfo.BusinessCategory_BaseList.forEach(item => {
                item.checked = This_Dealer_OtherInfo.BusinessCategory.split(',').indexOf(String(item.Key)) > -1;
              });
              this.DataBaseInfo.CompetingGoodsBrandA_BaseList.forEach(item => {
                item.checked = This_Dealer_OtherInfo.CompetingGoodsBrandA.split(',').indexOf(String(item.Key)) > -1;
              });
              this.DataBaseInfo.CompetingGoodsBrandB_BaseList.forEach(item => {
                item.checked = This_Dealer_OtherInfo.CompetingGoodsBrandB.split(',').indexOf(String(item.Key)) > -1;
              });
              this.DataBaseInfo.CompetingGoodsBrandC_BaseList.forEach(item => {
                item.checked = This_Dealer_OtherInfo.CompetingGoodsBrandC.split(',').indexOf(String(item.Key)) > -1;
              });

              this.$refs.basicForm.initForm({
                Dealer_BaseInfo: Double_Dealer_BaseInfo,
                Dealer_OtherInfo: This_Dealer_OtherInfo
              });
            }

            if (ShowLevel == 3) {
              this.$refs.storeForm.initForm({
                Dealer_BaseInfo: Door_Dealer_BaseInfo,
                Dealer_OtherInfo: This_Dealer_OtherInfo,
                DealerDetail_Audit: AuditDetail,
                AuditForList: AuditForList,
                ApplyForReasonId: AuditDetail.ApplyForReasonId,
                OtherNote: AuditDetail.OtherNote
              });
            }

            if (ShowLevel != 3) {
              this.DataBaseInfo.BusinessCategory_BaseList.forEach(item => {
                item.checked = This_Dealer_OtherInfo.BusinessCategory.split(',').indexOf(String(item.Key)) > -1;
              });
              this.DataBaseInfo.CompetingGoodsBrandA_BaseList.forEach(item => {
                if (This_Dealer_OtherInfo) {
                  item.checked = This_Dealer_OtherInfo.CompetingGoodsBrandA.split(',').indexOf(String(item.Key)) > -1;
                } else {
                  item.checked = false;
                }
              });
              this.DataBaseInfo.CompetingGoodsBrandB_BaseList.forEach(item => {
                if (This_Dealer_OtherInfo) {
                  item.checked = This_Dealer_OtherInfo.CompetingGoodsBrandB.split(',').indexOf(String(item.Key)) > -1;
                } else {
                  item.checked = false;
                }
              });
              this.DataBaseInfo.CompetingGoodsBrandC_BaseList.forEach(item => {
                if (This_Dealer_OtherInfo) {
                  item.checked = This_Dealer_OtherInfo.CompetingGoodsBrandC.split(',').indexOf(String(item.Key)) > -1;
                } else {
                  item.checked = false;
                }
              });
            }

            this.loadCity = true;
          }
        });
    },

    onSubmit() {
      let formParams = {};
      if (this.ShowLevel != 3) {
        formParams = Object.assign(
          this.form,
          this.$refs.basicForm.form.Dealer_BaseInfo,
          this.$refs.basicForm.form.Dealer_OtherInfo
        );
      } else {
        let storeForm = this.$refs.storeForm.form;
        formParams = { ...this.form, ...storeForm.Dealer_BaseInfo, ...storeForm.Dealer_OtherInfo };

        formParams.ApplyForReasonId = storeForm.ApplyForReasonId;
        formParams.Requirements = storeForm.Requirements;
        formParams.OtherNote = storeForm.OtherNote;
        formParams.Remark = storeForm.Remark;
      }

      formParams = { ...formParams, ...this.$refs.reviewForm.form };

      formParams.State = this.$refs.reviewForm.form.State;
      formParams.AuditRemark = this.$refs.reviewForm.form.AuditRemark;
      formParams.OldDoor = this.form.OldDoor;

      let flag = formParams.InitialStorePhoto.slice(0, 3).every(item => !!item.PhotoUrl);
      if (!flag) {
        this.$u.toast('必须上传门头照片、近景图、远景图');
        return false;
      }

      if (this.DealerGrade_Fill_In_Permissions > 0) {
        if (!formParams.DealerGrade) {
          this.$u.toast('请选择门店等级');
          return false;
        }
      }

      if (!formParams.State) {
        this.$u.toast('请选择审核状态');
        return false;
      }

      console.log(formParams);

      this.$u.api.approvalAddDealer(formParams).then(res => {
        const { ResultCode, ResultMessage } = res;
        if (ResultCode === 1) {
          this.$refs.uToast.show({
            title: ResultMessage,
            back: true
          });
        }
      });
    }
  }
};
</script>

<style lang="scss"></style>
